<?php

/**
 * Core Controller
 * 
 * @author lemon_cn
 * @version 2.0
 */
class CoreAction extends PublicAction
{

    public function searchindex()
    {
        /**
         * This function is the interface of search option.
         */
        $this -> _verifyPrivilege(D('Privilege') -> C('PRIVILEGE_PAYER'));
        $tmp = '<label for="from">From</label>
			<input type="text" id="from" name="from"/>
			<label for="to">to</label>
			<input type="text" id="to" name="to"/>';
        $navi = array(
            array(
                "URL" => U('Core/inquire'),
                "ID" => 'a1',
                "text" => "个人投资记录"),
            array(
                "URL" => U('Core/modify'),
                "ID" => 'a2',
                "text" => "基金财务记账"),
            array(
                "URL" => U('Core/getstatic'),
                "ID" => 'a3',
                "text" => "统计数据"));
        $temp = M('fund') -> find();
        $net = $temp['total'] / $temp['parts'];
        $anet = $temp['atotal'] / $temp['parts'];
        $net = sprintf('%.2f', $net);
        $anet = sprintf('%.2f', $anet);
        $this -> assign("netvalue", $net);
        $this -> assign("anetvalue", $anet);
        $this -> assign('UserNavi', $navi);
        $this -> display();
    }

    public function inquire()
    {
        /**
         * This function is the interface of inquiry
         */
        header('Content-Type:TEXT/HTML; charset=UTF-8');
        $this -> _verifyPrivilege(D('Privilegectrl') -> C('ADMISSION_VIEW_ACCOUNT_NETVALUE'));
        $this -> display();
    }

    public function TDX()
    {
        /**
         * This function is mapped with "TDX指数"
         */
        $this -> _verifyPrivilege(D('Privilegectrl') -> C('ADMISSION_VIEW_TDX'));
        $this -> display();
    }

    public function search()
    {
        /**
         * This function will return search result.
         * The Search Result will be used by Ajax
         */
    }

    public function buy()
    {
		 /**
		  * 
		  */
    }

    public function getRecord()
    {
        $Rec = D("Fundrecord");
        $result = $Rec -> where('rtime >= "' . $_POST['from'] . '"AND parts > 0 AND rtime <="' . $_POST['to'] . '" AND OperID = ' . $_SESSION['UID']) -> select();
        foreach ($result as $i)
        {
            $j = M('User') -> where('UID = ' . $i['operID']) -> find();
            echo '<tr><td width="30">' . $i['operID'] . '</td><td width="80">' . $j['username'] . '</td><td width="70">' . $j['truename'] . '</td><td width="160">' . $i["rtime"] . '</td><td width="50">' . abs($i['totalvalue']) . '</td><td width="70">' . (($i['totalvalue'] > 0) ? '申购' : '赎回') . '</td></tr>';
        }
    }

    public function modify()
    {
        $this -> _verifyPrivilege(D('Privilegectrl') -> C('ADMISSION_MODIFY_FUNDRECORD'));
        $this -> display();
    }

    public function getstatic()
    {
        $this -> _verifyPrivilege(D('Privilegectrl') -> C('ADMISSION_VIEW_FUNDSTATICS'));
        $this -> display();
    }

    public function getAvailable()
    {
        $tmp = M('User') -> where('UID = ' . $_POST['UID']) -> find();
        $PID = $tmp['PID'];
        $array = M('Privilege') -> where('PID <> 1') -> select();
        foreach ($array as $i)
        {
            if ($i['level'] <= $_SESSION['PLevel'] && $i['PID'] <> $PID)
            {
                echo '<option value="' . $i['PID'] . '">' . $i['description'] . '</option>';
            }
        }
    }

    public function TDXEstablish()
    {
        //$this -> _verifyPrivilege(D('Privilege') -> C('PRIVILEGE_MANAGER'));
        $this -> display();
    }

    public function jsonTDXList()
    {
        $page = $_POST['page'];//1 is the first page
        $perPage = $_POST['per'];
        $data = M('Tdxrate') -> order('rtime desc') -> limit($perPage) -> page($page) -> select();
        $User = M('User');
        foreach($data as $i)
        {
            $user = $User -> where("UID = ".$i['UID']) -> find();
            $i['UID'] = $user['username'];
        }
        $this ->  ajaxReturn($data);
    }
    
    public function jsonTDXPages()
    {
        $perPage = $_POST['per'];
        $sum = M('Tdxrate') -> count();
        if($sum % $perPage == 0)
            $this -> ajaxReturn($sum/$perPage);
        else
            $this -> ajaxReturn(floor($sum/$perPage) + 1);
    }
    
    public function postTDX()
    {
        $data = array();
        $data['UID'] = $_SESSION['UID'];
        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['__hash__'] = $_POST['hash'];
        $Rate = M("Tdxrate");
        if($Rate -> create($data))
        {
            $Rate -> add();
            $this -> ajaxReturn("", "增加记录成功！", 1);
        }
        else
            $this -> ajaxReturn("", $Rate->getError(), 0);
    }
    
    public function modifyTDX()
    {
        $ID = $_POST['ID'];
        $Rate = M('Tdxrate');
        $data = $Rate -> where("ID = ".$ID) -> find();
        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['ID'] = $ID;
        $Rate -> save($data);
        $this -> ajaxReturn("", "修改成功", 1);
    }
}

